package com.winning.sjzc.sjjflgl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.PascalNameFilter;
import com.winning.sjzc.common.utils.ResponseEntity;
import com.winning.sjzc.sjjflgl.entity.SJJFL;
import com.winning.sjzc.sjjflgl.service.DataSetClassificationService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/classification")
public class DataSetClassificationController {

    private static final Logger logger = LogManager.getLogger(DataSetClassificationController.class);

    @Autowired
    private DataSetClassificationService dsc;

    //查询
    @RequestMapping(value = "/queryDataSetClassification", method = RequestMethod.POST)
    public @ResponseBody
    String queryDataSetClassification(){
        List<SJJFL> list=new ArrayList<SJJFL>() ;
        try{
            list=dsc.queryDataSetClassification();
        }catch (Exception e){
            logger.error(e.getMessage(), e);
            return "";
        }
        return JSON.toJSONString(list,new PascalNameFilter());
    }


    //保存
    @RequestMapping(value = "/saveDataSetClassification", method = RequestMethod.POST)
    public @ResponseBody
    ResponseEntity saveDataSetClassification(@RequestBody SJJFL sjjfl){

        int res = -1;
        try{
            res = dsc.saveDataSetClassification(sjjfl);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseEntity.error();
        }
        return ResponseEntity.ok().put("res",res);
    }

    //修改
    @RequestMapping(value = "/updateDataSetClassification", method = RequestMethod.POST)
    public @ResponseBody
    ResponseEntity updateDataSetClassification(@RequestBody SJJFL sjjfl){

        int res = 2;
        try{
             dsc.updateDataSetClassification(sjjfl);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseEntity.error();
        }
        return ResponseEntity.ok().put("res",res);
    }

    //删除
    @RequestMapping(value = "/deleteDataSetClassification", method = RequestMethod.GET)
    public @ResponseBody
    ResponseEntity deleteDataSetClassification(String sjjfldm){
        try {
            dsc.deleteDataSetClassification(sjjfldm);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseEntity.error();
        }
        return ResponseEntity.ok();
    }
}
